<template>
  <div class="xtx-pay-page">
    <div class="container">
      <el-breadcrumb separator=">" style="height: 50px; line-height: 50px">
        <el-breadcrumb-item :to="{ path: '/cart' }">购物车</el-breadcrumb-item>
        <el-breadcrumb-item>支付订单</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 付款信息 -->
      <div class="pay-info">
        <span class="icon iconfont icon-queren2"></span>
        <div class="tip" v-if="leftTime > 0">
          <p>订单提交成功！请尽快完成支付。</p>
          <p>
            支付还剩
            <span style="color: #cf4444">{{ formatTime(leftTime) }}</span
            >, 超时后将取消订单
          </p>
        </div>
        <div class="amount">
          <span>应付总额：</span>
          <span>¥{{ order?.payMoney?.toFixed(2) }}</span>
        </div>
      </div>
      <!-- 付款方式 -->
      <div class="pay-type">
        <p class="head">选择以下支付方式付款</p>
        <div class="item">
          <p>支付平台</p>
          <a class="btn wx" href="javascript:;"></a>
          <a class="btn alipay" :href="payUrl"></a>
        </div>
        <div class="item">
          <p>支付方式</p>
          <a class="btn" href="javascript:;">招商银行</a>
          <a class="btn" href="javascript:;">工商银行</a>
          <a class="btn" href="javascript:;">建设银行</a>
          <a class="btn" href="javascript:;">农业银行</a>
          <a class="btn" href="javascript:;">交通银行</a>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup name="XtxPayPage">
import dayjs from "dayjs";

import { onMounted, ref } from "vue";
import { ApiRes } from "@/types/data";
import { useRoute, useRouter } from "vue-router";
import { OrderPayInfo } from "@/types/order";
import request from "@/utils/request";
import { ElMessage } from "element-plus";
import { useIntervalFn } from "@vueuse/core";
// import { baseURL } from "@/utils/request";
import { baseURL } from "@/utils/request";

const route = useRoute();
const router = useRouter();

// 支付宝支付之后，回跳的地址
const redirectUrl = encodeURIComponent(
  "http://www.corho.com:8080/#/pay/callback"
);
// 给支付宝图标绑定的地址
const payUrl = `${baseURL}pay/aliPay?orderId=${route.query.id}&redirect=${redirectUrl}`;

const id = route.query.id;
const order = ref<OrderPayInfo>({} as OrderPayInfo);

const leftTime = ref(0);

// 格式化时间（注意：vue3没有过滤器）
const formatTime = (time: number) => {
  return dayjs.unix(time).format("mm分ss秒");
};

onMounted(async () => {
  const res = await request.get<ApiRes<OrderPayInfo>>(`/member/order/${id}`);
  order.value = res.data.result;

  leftTime.value = res.data.result.countdown;

  const { pause } = useIntervalFn(() => {
    /* your function */
    leftTime.value--;

    if (leftTime.value <= 0) {
      pause();

      // 提示
      ElMessage.error("支付超时！");
      // 跳转到cart页
      router.replace("/cart");
    }
  }, 1000);
});
</script>

<style scoped lang="less">
.pay-info {
  background: #fff;
  display: flex;
  align-items: center;
  height: 240px;
  padding: 0 80px;
  .icon {
    font-size: 80px;
    color: #1dc779;
  }
  .tip {
    padding-left: 10px;
    flex: 1;
    p {
      &:first-child {
        font-size: 20px;
        margin-bottom: 5px;
      }
      &:last-child {
        color: #999;
        font-size: 16px;
      }
    }
  }
  .amount {
    span {
      &:first-child {
        font-size: 16px;
        color: #999;
      }
      &:last-child {
        color: @priceColor;
        font-size: 20px;
      }
    }
  }
}
.pay-type {
  margin-top: 20px;
  background-color: #fff;
  padding-bottom: 70px;
  p {
    line-height: 70px;
    height: 70px;
    padding-left: 30px;
    font-size: 16px;
    &.head {
      border-bottom: 1px solid #f5f5f5;
    }
  }
  .btn {
    width: 150px;
    height: 50px;
    border: 1px solid #e4e4e4;
    text-align: center;
    line-height: 48px;
    margin-left: 30px;
    color: #666666;
    display: inline-block;
    &.active,
    &:hover {
      border-color: @xtxColor;
    }
    &.alipay {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/7b6b02396368c9314528c0bbd85a2e06.png)
        no-repeat center / contain;
    }
    &.wx {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/c66f98cff8649bd5ba722c2e8067c6ca.jpg)
        no-repeat center / contain;
    }
  }
}
:deep(.el-breadcrumb__inner.is-link) {
  font-weight: 400;
}

:deep(.el-breadcrumb__inner a) {
  font-weight: 400;
}
</style>
